package dao;

import bean.Student;
import util.JDBCUtil1;

import java.io.IOException;
import java.sql.*;

public class StudentDao {

    Connection con = null;
    ResultSet rs = null;
    PreparedStatement pstm = null;
    Student student = null;
//    int row = 0;

    public Student student(String name) throws IOException{
        try {
            // 1.获取数据库连接
            con = JDBCUtil1.getCon();
            // 2.定义登陆的sql语句
//            Statement statement = con.createStatement();
            String sql = "select * from student where name=? ";

            // 3.获取预处理对象
            pstm = con.prepareStatement(sql);
            // 4.传参
            pstm.setObject(1,name);
            // 5.执行查询
            rs = pstm.executeQuery();
            // 6.解析结果集
            if (rs.next()){
                student = new Student();
                 //从结果集中获取数据，封装到实体类中
               student.setName(rs.getString("name"));
               student.setName(rs.getString("gpa"));
               student.setName(rs.getString("subject"));

               }
        } catch (Exception e){
            e.printStackTrace();
      }

//            ResultSet resultSet = statement.executeQuery(sql);
//            System.out.println("查询结果如下：");
//            while (resultSet.next()) {
//                System.out.println("name=" + resultSet.getString("name"));
//                System.out.println("GPA=" + resultSet.getString("gpa"));
//                System.out.println("Subject=" + resultSet.getString("subject"));
//
//            }
//            catch (Exception e){
//            e.printStackTrace();
//      }
            return student;
        }

//    public static void main(String[] args) {
//
//        System.out.println();
//    }
//    public int register(Student name) {
//        try{
//            // 1.获取数据库连接
//            con = JDBCUtil1.getCon();
//            // 2.定义注册的新增sql语句
//            String sql = "insert into student1(name) values(?)" ;
//            // 3.获取预处理对象
//            pstm = con.prepareStatement(sql);
//            // 4.传参
//            pstm.setObject(1,name.getName());
//            // 5.执行更新（增删改）
//            row = pstm.executeUpdate();
//        }catch (Exception e){
//            e.printStackTrace();
//        } finally {
//            JDBCUtil1.close(con,pstm);
//        }
//
//        return row;
//    }
}
